package project_part2;

import static org.junit.Assert.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import org.junit.Before;
import org.junit.Test;

public class FindMatchTest {

	static final int FN_BIT = 64;
	static final int MB_BIT = 32;
	static final int LN_BIT = 16;
	static final int DOB_BIT = 8;
	// static final int A_BIT = 8;
	static final int W_BIT = 4;
	static final int H_BIT = 2;
	static final int G_BIT = 1;

	/*
	 * @Before public void prepList() { ContactList cTestList = new
	 * ContactList();
	 * 
	 * for (int i = 0; i < 128; i++) { String firstName = "F"; String middleName
	 * = "F"; String lastName = "F"; String DOB = "10/10/2007";
	 * 
	 * short age = 200; short weight = 200; int height = 5; Gender gender =
	 * Gender.FEMALE;
	 * 
	 * Contact contact = null;
	 * 
	 * if ((FN_BIT & i) == 64) firstName = "T"; if ((MB_BIT & i) == 32)
	 * middleName = "T"; if ((LN_BIT & i) == 16) lastName = "T"; if ((DOB_BIT &
	 * i) == 8) DOB = "10/10/2001"; // if ((A_BIT & i) == 1) // age = 100; if
	 * ((W_BIT & i) == 4) weight = 100; if ((H_BIT & i) == 2) height = 6; if
	 * ((G_BIT & i) == 1) gender = Gender.MALE;
	 * 
	 * contact = new Contact(firstName, middleName, lastName, DOB);
	 * contact.setWeight(weight); contact.setHeight(height, height);
	 * contact.setGender(gender);
	 * 
	 * cTestList.add(contact); System.out.println(i + contact.toString());
	 * 
	 * } }
	 */
	/*
	 * @Test public void test() {
	 * 
	 * int score_base = 0; int outcome = 0;
	 * 
	 * ContactList clist = new ContactList(); Contact con = setContact(i);
	 * Contact res[] = null; MatchCriteria mc = setCriteria(j, false);
	 * 
	 * score_base = (x & y); score_base = sumBits(score_base);
	 * 
	 * if (score_base >= k) outcome = 1;
	 * 
	 * clist.add(con);
	 * 
	 * res = clist.findMatches(con, mc, k);
	 * 
	 * assertEquals(res.length, outcome);
	 * 
	 * }
	 */
	


	public boolean listMatches(Contact[] clist, int criteria) {

		SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");

		for (int i = 0; i < clist.length; i++) {

			if ((FN_BIT & criteria) == 64 && clist[i].getFirstName() != "T")
				return false;
			if ((MB_BIT & criteria) == 32 && clist[i].getMiddleName() != "T")
				return false;
			if ((LN_BIT & criteria) == 16 && clist[i].getLastName() != "T")
				return false;
			try {
				if ((DOB_BIT & criteria) == 8
						&& clist[i].getDOB().equals(
								dateFormat.parse("10/10/2001")) == false)
					return false;
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			// if ((A_BIT & i) == 1)
			// age = 100;
			if ((W_BIT & criteria) == 4 && clist[i].getWeight() != 100)
				return false;
			if ((H_BIT & criteria) == 2 && clist[i].getHeight().getFeet() != 5)
				return false;
			if ((G_BIT & criteria) == 1 && clist[i].getGender() != Gender.MALE)
				return false;
		}

		return true;
	}

	public int calculateScore(Contact con, int criteria) {

		SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
		int score = 0;

		if ((FN_BIT & criteria) == 64 && con.getFirstName() == "T")
			score++;
		if ((MB_BIT & criteria) == 32 && con.getMiddleName() == "T")
			score++;
		if ((LN_BIT & criteria) == 16 && con.getLastName() == "T")
			score++;
		try {
			if ((DOB_BIT & criteria) == 8
					&& con.getDOB().equals(dateFormat.parse("10/10/2001")) == true)
				score++;
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// if ((A_BIT & i) == 1)
		// age = 100;
		if ((W_BIT & criteria) == 4 && con.getWeight() == 100)
			score++;
		if ((H_BIT & criteria) == 2 && con.getHeight().getFeet() == 5)
			score++;
		if ((G_BIT & criteria) == 1 && con.getGender() == Gender.MALE)
			score++;

		return score;
	}

	public Contact setContact(int num) {
		Contact c;

		String DOB = "10/10/2007";
		short weight = 200;
		int height = 5;
		Gender gender = Gender.FEMALE;

		if ((DOB_BIT & num) == 8)
			DOB = "10/10/2001";
		// if ((A_BIT & i) == 1)
		// age = 100;
		if ((W_BIT & num) == 4)
			weight = 100;
		if ((H_BIT & num) == 2)
			height = 6;
		if ((G_BIT & num) == 1)
			gender = Gender.MALE;

		c = new Contact("T", "T", "T", DOB);
		c.setWeight(weight);
		c.setHeight(height, height);
		c.setGender(gender);

		return c;
	}

	public MatchCriteria setCriteria(int num, boolean inv) {
		MatchCriteria mc = new MatchCriteria();

		Gender gender = Gender.FEMALE;


			mc.ageHigh = 6;
			mc.ageLow = 4;
			mc.matchAge = inv;

			mc.weightHigh = 250;
			mc.weightLow = 150;
			mc.matchWeight = inv;

			mc.heightHigh = new ContactHeight(5, 8);
			mc.heightLow = new ContactHeight(5, 2);
			mc.matchHeight = inv;

			gender = Gender.FEMALE;
			mc.matchGender = inv;


	

		if ((DOB_BIT & num) == 8) {
			mc.ageHigh = 12;
			mc.ageLow = 10;
			mc.matchAge = true;
		}
		if ((W_BIT & num) == 4) {
			mc.weightHigh = 150;
			mc.weightLow = 50;
			mc.matchWeight = true;
		}
		if ((H_BIT & num) == 2) {
			mc.heightHigh = new ContactHeight(6, 9);
			mc.heightLow = new ContactHeight(6, 3);
			mc.matchHeight = true;
		}
		if ((G_BIT & num) == 1) {
			gender = Gender.MALE;
			mc.matchGender = true;
		}

		mc.addGender(gender);
		return mc;
	}

	public int sumBits(int value) {
		int total = 0;

		if ((1 & value) == 1)
			total++;

		for (int i = 0; i < 31; i++) {
			value = value >> 1;

			if ((1 & value) == 1)
				total++;

		}

		return total;
	}
}
